package com.qf.joint.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.joint.pojo.Recruiter;
import com.qf.joint.pojo.Recruitpost;
import com.qf.joint.pojo.Tradepost;
import com.qf.joint.service.RecruiterService;
import com.qf.joint.utils.Info;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/recruiter")
@CrossOrigin
public class RecruiterController {
    @Autowired
    private RecruiterService recruiterService;
    @ResponseBody
    @RequestMapping("/all/{rid}")
    public Info<Recruiter> select(@PathVariable("rid")int rid){
        Recruiter recruiter=new Recruiter();
        recruiter.setRid(rid);
        List<Recruiter> list = recruiterService.select(recruiter);
       /* Recruiter insert=new Recruiter();
        insert.setCid(1);
        insert.setLogo("999");
        insert.setRid(4);
        Recruiter update=new Recruiter();
        update.setRid(1);
        update.setLogo("10000");
        List<Integer> num=new ArrayList<>();
        num.add(3);
        int i=recruiterService.delete(num);
        int y=recruiterService.insert(insert);
        int k=recruiterService.update(update);
        System.out.println(i);
        System.out.println(y);
        System.out.println(k);*/
        Info<Recruiter> info=new Info<>();
        info.setData(list);
        info.setMsg("没有查到数据");
        info.setCode(0);
        info.setCount(list.size());
        return info;
    }
    @RequestMapping(value = "/select",method = RequestMethod.GET)
    @ResponseBody
    public Info<Recruiter> selectAll(@RequestParam("page") int page, @RequestParam("limit") int limit){
        PageHelper.startPage(page, limit);
        Recruiter recruiter=new Recruiter();
        List<Recruiter> list=recruiterService.select(recruiter);
        PageInfo<Recruiter> pageInfo=new PageInfo<>(list);
        Info<Recruiter> info=new Info<>();
            info.setData(list);

            info.setMsg("没有查到数据");
        if (null != list && list.size() > 0) {
            info.setCode(0);
        }else info.setCode(1);
        info.setCount((int) pageInfo.getTotal());
       // info.setCount(list.size());
        return info;
    }
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    public String insertNew(Recruiter recruiter){
        int k=recruiterService.insert(recruiter);
        return "redirect:/admin/admin";
    }
    @RequestMapping(value = "/update",method = RequestMethod.GET)
    public String updateNew(Recruiter recruiter){
        int k=recruiterService.update(recruiter);
        return "redirect:/admin/admin";
    }
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public String deleteNew(String idArry){
        String[] rpid = idArry.split(",");
        System.out.println(Arrays.toString(rpid));
        List<Integer> list=new ArrayList<>();
        for(int i=0;i<rpid.length;i++){
            list.add(Integer.valueOf(rpid[i]));
        }
        int k=recruiterService.delete(list);
        return "redirect:/admin/admin";
    }
}
